package com.uyun.rbac.user.dao.impl;

import com.uyun.rbac.permission.dao.PermissionDao;
import com.uyun.rbac.permission.entity.GroupEntity;
import com.uyun.rbac.permission.entity.RoleEntity;
import com.uyun.rbac.permission.entity.UserEntity;
import com.uyun.rbac.user.dao.UserDao;
import com.uyun.util.AppUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Repository
public class UserDaoImpl implements UserDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private PermissionDao permissionDao;
	
	@Override
	public List<UserEntity>  getUserList(Map<String,Object> paraMap){
		
		String username = (String) paraMap.get("username");
		String status = (String) paraMap.get("status");
		String groups = (String) paraMap.get("groups");

		StringBuilder sb = new StringBuilder();
		List<Object> listStr = new ArrayList<>();
		
		if(AppUtils.StringUtil(username)!=null){
			sb.append(" and username  like  ? ");
			listStr.add(username+"%");//可以用到索引
		}

        if(AppUtils.StringUtil(status)!=null&&!"-1".equals(status)){
            sb.append(" and status =  ? ");
            listStr.add(status);//可以用到索引
        }

        if(AppUtils.StringUtil(groups)!=null&&!"-1".equals(groups)){
            sb.append(" and groupid  =  ? ");
            listStr.add(groups);//可以用到索引
        }


		String sql = " select id,groupid,username,status,remark,lastlogin_ip from user where 1=1 ";

        List<UserEntity> dataList = new ArrayList<>();

        jdbcTemplate.query(sql+sb.toString(),(ResultSet rs )->{
            UserEntity userEntity = new UserEntity();
            userEntity.setId(rs.getInt("id"));
            int groupId = rs.getInt("groupid");
            userEntity.setGroupId(groupId);
            userEntity.setUserName(rs.getString("username"));
            userEntity.setStatus(rs.getInt("status"));
            userEntity.setRemark(rs.getString("remark"));
            userEntity.setLastlogin_ip(rs.getString("lastlogin_ip"));
            GroupEntity groupEntity = getGroupEntityById(groupId);
            //userEntity.setGroupEntity(groupEntity);
            userEntity.setGroupName(groupEntity.getRemark());
            dataList.add(userEntity);

        },listStr.toArray());


		return dataList;
		
	}


	@Override
	public UserEntity getUserEntityByUserName(String userName){

		Integer id ;
		try{
			id = jdbcTemplate.queryForObject(" select id from user  where username = ? ",Integer.class,userName);

		}catch (Exception e){
			return null;
		}

		if(id==null|| id ==0){
			return null;
		}else{
			return getUserEntityById(id);
		}

	}


	@Override
	public List<GroupEntity> getAllUserGroups(){

		List<GroupEntity> list = new ArrayList<>();

		jdbcTemplate.query("select id,name,remark from  `group` ",(ResultSet rs)->{

			int id = rs.getInt("id");
			String name = rs.getString("name");
			String remark = rs.getString("remark");
			GroupEntity entity = new GroupEntity();
			entity.setId(id);
			entity.setName(name);
			entity.setRemark(remark);

			list.add(entity);
		});

		return list;

	}


	@Override
	public UserEntity getUserEntityById(int id){

		UserEntity userEntity = new UserEntity();

		jdbcTemplate.query(" select * from  user  where id = ? ",(ResultSet rs )->{

			userEntity.setId(id);
			int groupId = rs.getInt("groupid");
			userEntity.setGroupId(groupId);
			userEntity.setUserName(rs.getString("username"));
			userEntity.setPassword(rs.getString("password"));
			userEntity.setStatus(rs.getInt("status"));
			userEntity.setRemark(rs.getString("remark"));
			userEntity.setLastlogin_ip(rs.getString("lastlogin_ip"));

			GroupEntity groupEntity = getGroupEntityById(groupId);
			userEntity.setGroupEntity(groupEntity);

		},id);

		return userEntity;

	}



	@Override
	public GroupEntity getGroupEntityById(int id){

		GroupEntity groupEntity = new GroupEntity();

		jdbcTemplate.query(" select * from  `group`  where id=? ",(ResultSet rs )->{


			groupEntity.setId(id);
			groupEntity.setName(rs.getString("name"));
			groupEntity.setRemark(rs.getString("remark"));
			List<RoleEntity> list = new ArrayList<>();

			jdbcTemplate.query("select gr.roleid from  role r " +
					"LEFT JOIN  group_role gr on gr.roleid=r.id " +
					"where gr.groupid=? ",(ResultSet innerRs )->{

				int roleId = innerRs.getInt("roleid");
				RoleEntity roleEntity = permissionDao.getRoleEntityById(roleId);
				list.add(roleEntity);


			},id);

			groupEntity.setRoleEntityList(list);

		},id);

		return groupEntity;

	}


    @Override
    public int uptUserGroup(String userId,String groupId){

        return jdbcTemplate.update("update  user set groupid=? where id =?  ",groupId,userId);

    }


}
